import { ref, createApp, nextTick } from 'vue';

// 弹窗管理器类
class DialogManager {
    constructor() {
        // 游戏结束弹窗状态
        this.gameOverVisible = ref(false);
        
        this.gameOverData = ref({
            sessionDuration: 0,
            consumedCoins: 0
        });
        this.gameOverCloseCallback = null;
        
        // 关闭客户端弹窗状态
        this.closeClientVisible = ref(false);
        this.closeClientStatus = ref(1);
        this.closeClientParams = ref({});
        this.closeClientCallback = null;
        
        // 账号资源不足弹窗状态
        this.resourceInsufficientVisible = ref(false);
        this.resourceInsufficientCloseCallback = null;
        this.resourceInsufficientRechargeCallback = null;
    }
    
    // 显示游戏结束弹窗
  showGameOverDialog(gameData, onClose) {
    // 解构游戏数据，设置默认值
    const { sessionDuration = 0, consumedCoins = 0 } = gameData || {};
    // 更新游戏数据
    this.gameOverData.value = {
      sessionDuration: Number(sessionDuration),
      consumedCoins: Number(consumedCoins)
    };
    // 设置关闭回调
    this.gameOverCloseCallback = onClose || (() => {});
    // 确保游戏结束弹窗可见
    this.gameOverVisible.value = true;
  }
    
    // 隐藏游戏结束弹窗
    hideGameOverDialog() {
        this.gameOverVisible.value = false;
        if (this.gameOverCloseCallback) {
            this.gameOverCloseCallback();
            this.gameOverCloseCallback = null;
        }
    }
    
    // 显示关闭客户端弹窗
    showCloseClientDialog(status, params, onClose) {
        this.closeClientStatus.value = status;
        this.closeClientParams.value = { ...params };
        this.closeClientCallback = onClose;
        this.closeClientVisible.value = true;
    }
    
    // 隐藏关闭客户端弹窗
    hideCloseClientDialog() {
        this.closeClientVisible.value = false;
        if (this.closeClientCallback) {
            this.closeClientCallback(this.closeClientStatus.value);
            this.closeClientCallback = null;
        }
    }
    
    // 显示账号资源不足弹窗
    showResourceInsufficientDialog(onClose, onRecharge) {
        this.resourceInsufficientCloseCallback = onClose;
        this.resourceInsufficientRechargeCallback = onRecharge;
        this.resourceInsufficientVisible.value = true;
    }
    
    // 隐藏账号资源不足弹窗
    hideResourceInsufficientDialog() {
        this.resourceInsufficientVisible.value = false;
        if (this.resourceInsufficientCloseCallback) {
            this.resourceInsufficientCloseCallback();
            this.resourceInsufficientCloseCallback = null;
        }
    }
    
    // 处理账号资源不足弹窗的充值按钮点击
    handleResourceInsufficientRecharge() {
        this.resourceInsufficientVisible.value = false;
        if (this.resourceInsufficientRechargeCallback) {
            this.resourceInsufficientRechargeCallback();
            this.resourceInsufficientRechargeCallback = null;
        }
    }
    
    // 获取响应式状态（用于组件内部）
    getReactiveState() {
        // 移除强制将gameOverVisible设置为false的逻辑，允许通过路由参数正常显示弹窗
        return {
            gameOverVisible: this.gameOverVisible,
            gameOverData: this.gameOverData,
            closeClientVisible: this.closeClientVisible,
            closeClientStatus: this.closeClientStatus,
            closeClientParams: this.closeClientParams,
            resourceInsufficientVisible: this.resourceInsufficientVisible
        };
    }
}

// 导出单例实例
const dialogManager = new DialogManager();
export default dialogManager;